<?php
header("Content-type: image/png");
require_once("../includes/config.in.php");
require_once("../includes/function.in.php");
session_start();

$_SERVER['DOCUMENT_ROOT'] = dirname(dirname(__FILE__));
$targetPath = $_SERVER['DOCUMENT_ROOT'];

function generateCode($characters) {
    mb_internal_encoding('UTF-8');
    mb_regex_encoding('UTF-8');
    $possible = 'ฟผหปกภพดอถทครมตนสจยวขบงชลฐญซศณษฒธฌฮฑฉฆฤ';
    $count_mb = mb_strlen($possible);
    $code = '';
    $i = 0;
    while ($i < $characters) { 
        $random_position = mt_rand(0, ($count_mb-1));
            $code .= mb_substr($possible, $random_position, 1, 'UTF-8');
            $i++;
    }
    return $code;
}

$font = $targetPath."/capcha/font/angsab.ttf";
$_SESSION["security_code"] = $code = generateCode($_GET['characters']);
$im = imagecreate($_GET['width'], $_GET['height']);  
$white = ImageColorAllocate($im, 255, 255, 255); 
$black = ImageColorAllocate($im, 0, 0, 0); 
imagefill($im, 0, 0, $black);
imageTTFText($im, 20, 0, 6, 18, $white, $font, $code);
imagepng($im); 
imagedestroy($im);